<?php

/**
 * web login model （网页授权登录）
 */
class wloginModel
{

    public $base;

    /**
     * 第三方平台信息
     * @var array
     */
    static public $platForm = array
    (
        'qq' => array('appid' => '101233736',
                      'appkey' => 'ae51175e56c931e6a235e5cc98ec6998',
                      'authUri' => 'https://graph.z.qq.com/moc2/authorize?response_type=code&state=irun&client_id=%s&redirect_uri=%s&scope=%s&display=mobile',
                      'tokenUri' => 'https://graph.z.qq.com/moc2/token?grant_type=authorization_code&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s',
                      'openidUri' => 'https://graph.z.qq.com/moc2/me?access_token=%s'
                      ),

        'weibo' => array('appid' => '1741346638',
                         'appkey' => '4d6c057446772f34fb084915b8f97565',
                         'authUri' => 'https://open.weibo.cn/oauth2/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=irun&display=mobile',
                         'tokenUri' => 'https://api.weibo.com/oauth2/access_token',

                         ),  
    );

    public function __construct(& $base)
    {
        $this->base = $base;
    }

    /**
     * 生成授权申请链接
     * @param string type 平台标识
     * @param string redirectUri  回调地址
     * @return string oauthUri
     */
    public function getAuthUri($type, $redirectUri)
    {
        $oauthUri = '';
         
        $platFormInfo = isset(self::$platForm[$type]) ? self::$platForm[$type] : array();

        if(!empty($platFormInfo))
        {
            switch ($type) {
                case 'qq':
                    $oauthUri = sprintf($platFormInfo['authUri'], $platFormInfo['appid'], $redirectUri, '');
                    break;
                case 'weibo':
                    $oauthUri = sprintf($platFormInfo['authUri'], $platFormInfo['appid'], $redirectUri, '');
                    break;
                
                default:
                    # code...
                    break;
            }
        }

        return $oauthUri;
    }
    /**
     * 获取 access_token
     * @param string type 平台标识
     * @param string redirectUri  回调地址 (不知道有蛋用)
     * @return string access_token
     */
    public function getAccessToken($type, $code, $redirectUri)
    {
        $relFormat = array();

        $platFormInfo = isset(self::$platForm[$type]) ? self::$platForm[$type] : array();

        if(!empty($platFormInfo))
        {
            // 生成 获取access_token 的url
            $tokenUri = '';
            switch ($type) {
                case 'qq':
                    $tokenUri = $platFormInfo['tokenUri'];
                    $param = array('client_id' => $platFormInfo['appid'],
                                   'client_secret' => $platFormInfo['appkey'],
                                   'grant_type' => 'authorization_code',
                                   'code' => $code,
                                   'redirect_uri' => $redirectUri,
                                );
                    $method = 'GET';
                    break;
                case 'weibo':
                    $tokenUri = $platFormInfo['tokenUri'];
                    $param = array('client_id' => $platFormInfo['appid'],
                                   'client_secret' => $platFormInfo['appkey'],
                                   'grant_type' => 'authorization_code',
                                   'code' => $code,
                                   'redirect_uri' => $redirectUri,
                                );
                    $method = 'POST';
                    break;
                
                default:
                    # code...
                    break;
            }

            if($tokenUri)
            {
                $rel = common::getContents($tokenUri, $param, 10, $method);
                // qq: access_token=FE04************************CCE2&expires_in=7776000&refresh_token=88E4************************BE14
                
                // 日志
                $logInfo = '['.date("Y-m-d H:i:s").'] ['. $type .'] '.$rel;
                $this->base->load('log')->writeLog('oauth', $logInfo, 'access_token.txt');

                if($type = 'qq')
                {
                    parse_str($rel, $relFormat);
                }
                else if(common::is_json($rel))
                {
                    $relFormat = json_decode($rel, true);
                }
            }
        }


        return $relFormat;
    }
    /**
     * 生成授权申请链接
     * @param string type 平台标识
     * @return string oauthUri
     */
    public function getOpenId($type, $access_token)
    {
        $openidUri = '';
        $openid = '';
         
        $platFormInfo = isset(self::$platForm[$type]) ? self::$platForm[$type] : array();

        if(!empty($platFormInfo))
        {
            switch ($type) {
                case 'qq':
                    $openidUri = sprintf($platFormInfo['openidUri'], $access_token);
                    break;
                
                default:
                    # code...
                    break;
            }
            if($openidUri)
            {
                $rel = common::getContents($openidUri);
                // client_id=100222222&openid=1704************************878C

                // 日志
                $logInfo = '['.date("Y-m-d H:i:s").'] ['. $type .'] '.$rel;
                $this->base->load('log')->writeLog('oauth', $logInfo, 'openid.txt');

                parse_str($rel, $relFormat);
                if(isset($relFormat['openid']))
                {
                    return $relFormat['openid'];
                }
            }
        }

        return $openid;
    }

}

?>